package routines;

// Generated 18.12.2014 0:49:09 by Hibernate Tools 4.3.1

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

/**
 * Legalentity generated by hbm2java
 */
@Entity
@NamedQueries({
		@NamedQuery(name = "LegalEntity.getByInnKpp", query = "SELECT le from LegalEntity le where le.inn = :inn and le.kpp= :kpp"),
		@NamedQuery(name = "LegalEntity.getByOgrn", query = "SELECT le from LegalEntity le where le.ogrn = :ogrn"),
		@NamedQuery(name = "LegalEntity.getByInnKppOgrn", query = "SELECT le from LegalEntity le where le.ogrn = :ogrn and le.inn= :inn and le.kpp = :kpp")

})
@Table(name = "legalentity", indexes = { @Index(columnList = "ogrn", name = "idx_ogrn") })
public class LegalEntity extends Print {

	private long legalentityId;
	private String fullname;
	private String shortname;
	private String inn;
	private String kpp = "-";
	private String ogrn;
	private String factualaddress;
	private String postaddress;
	private String addinfo;
	private String sourceType;
	private Date inserted = new Date();
	private List<ContactInfo> contacts = null;
	private String innOld;
	private List<Notification> notifications;
	private LoadFrom loadfrom;

	private static final Pattern innPatter = Pattern.compile("\\d{10}|\\d{12}");

	public static boolean isValidINN(String inn) {
		if (inn == null)
			return false;
		if (!innPatter.matcher(inn).matches()) {
			return false;
		}
		int length = inn.length();
		if (length == 12) {
			return INNStep(inn, 2, 1) && INNStep(inn, 1, 0);
		} else {
			return INNStep(inn, 1, 2);
		}
	}

	private static final int[] checkArr = new int[] { 3, 7, 2, 4, 10, 3, 5, 9,
			4, 6, 8 };

	private static boolean INNStep(String inn, int offset, int arrOffset) {
		int sum = 0;
		int length = inn.length();
		for (int i = 0; i < length - offset; i++) {
			sum += (inn.charAt(i) - '0') * checkArr[i + arrOffset];
		}
		return (sum % 11) % 10 == inn.charAt(length - offset) - '0';
	}

	public LoadFrom getLoadfrom() {
		return loadfrom;
	}

	public void setLoadfrom(LoadFrom loadfrom) {
		this.loadfrom = loadfrom;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
	@Cascade({ CascadeType.SAVE_UPDATE })
	public List<Notification> getNotifications() {
		return notifications;
	}

	public void setNotifications(List<Notification> notifications) {
		this.notifications = notifications;
	}

	public LegalEntity() {
	}

	public void AddContact(ContactInfo c) {
		if (contacts == null)
			contacts = new ArrayList<ContactInfo>();
		c.setLegalentity(this);
		contacts.add(c);
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "legalentity")
	@Cascade({ CascadeType.SAVE_UPDATE })
	@OnDelete(action = OnDeleteAction.CASCADE)
	public List<ContactInfo> getContacts() {
		return contacts;
	}

	public void setContacts(List<ContactInfo> contacts) {
		this.contacts = contacts;
	}

	public LegalEntity(long legalentityId, String fullname, String shortname,
			String inn, String kpp, String ogrn, String factualaddress,
			String postaddress, String addinfo, String sourceType,
			Date inserted, String innOld) {
		this.legalentityId = legalentityId;
		this.fullname = fullname;
		this.shortname = shortname;
		this.inn = inn;
		this.kpp = kpp;
		this.ogrn = ogrn;
		this.factualaddress = factualaddress;
		this.postaddress = postaddress;
		this.addinfo = addinfo;
		this.sourceType = sourceType;
		this.inserted = inserted;
		this.innOld = innOld;
	}

	public String toString() {
		StringBuilder s = new StringBuilder();
		s.append(", shortname: ").append(shortname).append(", inn: ")
				.append(inn).append(", kpp: ").append(kpp).append(", ogrn: ")
				.append(ogrn);
		// .append(", : ").append()
		return s.toString();
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_legalentity_gen")
	@SequenceGenerator(name = "seq_legalentity_gen", sequenceName = "seq_legalentity")
	@Column(name = "legalentity_id")
	public long getLegalentityId() {
		return this.legalentityId;
	}

	public void setLegalentityId(long legalentityId) {
		this.legalentityId = legalentityId;
	}

	@Column(name = "fullname", columnDefinition = "TEXT")
	public String getFullname() {
		return this.fullname;
	}

	public LegalEntity setFullname(String fullname) {
		this.fullname = fullname;
		return this;
	}

	@Column(name = "shortname", columnDefinition = "TEXT")
	public String getShortname() {
		return this.shortname;
	}

	public LegalEntity setShortname(String shortname) {
		this.shortname = shortname;
		return this;
	}

	@Column(name = "inn", columnDefinition = "TEXT")
	public String getInn() {
		return this.inn;
	}

	public LegalEntity setInn(String inn) {
		this.inn = inn;
		return this;
	}

	public LegalEntity setInnWithCheck(String inn) {
		if (isValidINN(inn))
			this.inn = inn;
		else {
			this.innOld = inn;
			this.inn = "B-" + HibernateUtil.getBadINN().toString();
		}
		return this;
	}

	@Column(name = "kpp", columnDefinition = "TEXT")
	public String getKpp() {
		return this.kpp;
	}

	public LegalEntity setKpp(String kpp) {
		this.kpp = kpp == null ? "-" : kpp;
		return this;
	}

	@Column(name = "ogrn", columnDefinition = "TEXT")
	public String getOgrn() {
		return this.ogrn;
	}

	public LegalEntity setOgrn(String ogrn) {
		this.ogrn = ogrn;
		return this;
	}

	@Column(name = "factualaddress", columnDefinition = "TEXT")
	public String getFactualaddress() {
		return this.factualaddress;
	}

	public LegalEntity setFactualaddress(String factualaddress) {
		this.factualaddress = factualaddress;
		return this;
	}

	@Column(name = "postaddress", columnDefinition = "TEXT")
	public String getPostaddress() {
		return this.postaddress;
	}

	public LegalEntity setPostaddress(String postaddress) {
		this.postaddress = postaddress;
		return this;
	}

	@Column(name = "addinfo", columnDefinition = "TEXT")
	public String getAddinfo() {
		return this.addinfo;
	}

	public LegalEntity setAddinfo(String addinfo) {
		this.addinfo = addinfo;
		return this;
	}

	@Column(name = "source_type", columnDefinition = "TEXT")
	public String getSourceType() {
		return this.sourceType;
	}

	public LegalEntity setSourceType(String sourceType) {
		this.sourceType = sourceType;
		return this;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "inserted")
	public Date getInserted() {
		return this.inserted;
	}

	public LegalEntity setInserted(Date inserted) {
		this.inserted = inserted;
		return this;
	}

	@Column(name = "inn_old", columnDefinition = "TEXT")
	public String getInnOld() {
		return this.innOld;
	}

	public LegalEntity setInnOld(String innOld) {
		this.innOld = innOld;
		return this;
	}

}
